﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;
namespace Com.GUI.LibraryManager.Controls.SubControl
{
    /// <summary>
    /// Interaction logic for ucAirConditionRoofSurface.xaml
    /// </summary>
    public partial class ucAirConditionRoofSurface : UserControl
    {
        public ucAirConditionRoofSurface()
        {
            InitializeComponent();
            loadData();
        }

        private void cbxSurface_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxSurface.SelectedItem != null)
            {
                txtRadiation.Text = ((tb_roof_surface)cbxSurface.SelectedItem).absorption_coefficient.ToString();
            }
        }

        private void loadData()
        {
            cbxSurface.Items.Clear();            
            foreach (var item in LibraryManagerInCom.RoofSurfaceList)
            {
                cbxSurface.Items.Add(item);
            }
            cbxSurface.DisplayMemberPath = "surface_roof";
            cbxSurface.SelectedIndex = 0;
        }

        public void saveData()
        {
            try
            {
                tb_roof_surface rs = new tb_roof_surface();
                rs.surface_roof = cbxSurface.Text;
                rs.absorption_coefficient = Convert.ToDouble(txtRadiation.Text);
                LibraryManagerInCom.addRoofSurface(rs);
                loadData();
             
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void addData()
        {
            cbxSurface.Text = "";
            txtRadiation.Text = "";
        
        }
        public void delData()
        {
            if (MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                try
                {
                    tb_roof_surface rs = (tb_roof_surface)cbxSurface.SelectedItem;
                    LibraryManagerInCom.delRoofSurface(rs);
                    loadData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
          
        }
        public void copyData()
        {
            tb_roof_surface rs = LibraryManagerInCom.copyRoofSurface((tb_roof_surface)cbxSurface.SelectedItem);
            cbxSurface.Text = rs.surface_roof;
            txtRadiation.Text = rs.absorption_coefficient.ToString();
        }
        public void cancel()
        {
            loadData();
        
        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
    }
}
